<?php

/**
 * Employee form base class.
 *
 * @method Employee getObject() Returns the current form's model object
 *
 * @package    Amawal Invest
 * @subpackage form
 * @author     Ameen Shoman<amin.shoman@gmail.com>
 */
abstract class BaseEmployeeForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                       => new sfWidgetFormInputHidden(),
      'code'                     => new sfWidgetFormInputText(),
      'email'                    => new sfWidgetFormInputText(),
      'card_id'                  => new sfWidgetFormInputText(),
      'department_id'            => new sfWidgetFormPropelChoice(array('model' => 'CodeDepartment', 'add_empty' => true)),
      'section_id'               => new sfWidgetFormPropelChoice(array('model' => 'CodeSection', 'add_empty' => true)),
      'mobil_no'                 => new sfWidgetFormInputText(),
      'hiring_date'              => new sfWidgetFormDate(),
      'birth_date'               => new sfWidgetFormDate(),
      'branch_id'                => new sfWidgetFormPropelChoice(array('model' => 'Branch', 'add_empty' => true)),
      'in_active'                => new sfWidgetFormInputCheckbox(),
      'link_employee_leave_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'CodeLeaveType')),
    ));

    $this->setValidators(array(
      'id'                       => new sfValidatorPropelChoice(array('model' => 'Employee', 'column' => 'id', 'required' => false)),
      'code'                     => new sfValidatorString(array('max_length' => 255)),
      'email'                    => new sfValidatorString(array('max_length' => 255)),
      'card_id'                  => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647)),
      'department_id'            => new sfValidatorPropelChoice(array('model' => 'CodeDepartment', 'column' => 'id', 'required' => false)),
      'section_id'               => new sfValidatorPropelChoice(array('model' => 'CodeSection', 'column' => 'id', 'required' => false)),
      'mobil_no'                 => new sfValidatorString(array('max_length' => 11, 'required' => false)),
      'hiring_date'              => new sfValidatorDate(array('required' => false)),
      'birth_date'               => new sfValidatorDate(array('required' => false)),
      'branch_id'                => new sfValidatorPropelChoice(array('model' => 'Branch', 'column' => 'id', 'required' => false)),
      'in_active'                => new sfValidatorBoolean(array('required' => false)),
      'link_employee_leave_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'CodeLeaveType', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorAnd(array(
        new sfValidatorPropelUnique(array('model' => 'Employee', 'column' => array('code'))),
        new sfValidatorPropelUnique(array('model' => 'Employee', 'column' => array('card_id'))),
      ))
    );

    $this->widgetSchema->setNameFormat('employee[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Employee';
  }

  public function getI18nModelName()
  {
    return 'EmployeeI18n';
  }

  public function getI18nFormClass()
  {
    return 'EmployeeI18nForm';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['link_employee_leave_list']))
    {
      $values = array();
      foreach ($this->object->getLinkEmployeeLeaves() as $obj)
      {
        $values[] = $obj->getLeaveTypeId();
      }

      $this->setDefault('link_employee_leave_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveLinkEmployeeLeaveList($con);
  }

  public function saveLinkEmployeeLeaveList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['link_employee_leave_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(LinkEmployeeLeavePeer::EMPLOYEE_ID, $this->object->getPrimaryKey());
    LinkEmployeeLeavePeer::doDelete($c, $con);

    $values = $this->getValue('link_employee_leave_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new LinkEmployeeLeave();
        $obj->setEmployeeId($this->object->getPrimaryKey());
        $obj->setLeaveTypeId($value);
        $obj->save();
      }
    }
  }

}
